/*
 * =====================================================================================
 *
 *       Filename:  init_in_c.c
 *
 *    Description:  This experiments contain some experiments with
 *    initializing stuff in C.
 *
 *        Version:  1.0
 *        Created:  Tuesday 09 August 2011 02:50:22  IST
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Dilawar Singh (Graduate Student, EE IITB), dilawar@ee.iitb.ac.in
 *      Institute:  IIT Bombay
 *
 * =====================================================================================
 */

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

char dilawar[5];

/*  what about an structure */
typedef struct node
{
    int val;
    struct node* lnode;
    struct node* rnode;
} myNode;

myNode* nodeAvail;
myNode* pMyNode2;
char* pMyNode1;
int main()
{
    printf("Duniya Walo..\n");

    /*  lets initialize an array and check for its value */
    char* myString;
    int* pInt; /*  this does not initialize pInt to 0 */
    int* pInt0 = calloc((size_t)pInt0, sizeof(int)); /*  it does initialize it to zero */
    int i;
    pInt = &i;
    printf("The value of non-initialised i is %d\n", i);

    for(i = 0; i < 10; i++) /*  when for-loop in uninitialised.  */
    {
            printf("|- myString[%d] is %d\n", i, myString[i]);
            printf("\t|- *pInt %d\n", *pInt);
    }

    /*  Let see what happens to global variables. */
    printf("\nGloabal variable,\n");
    int j = 0;
    unsigned long long loop = 0;

    /*  let initialise dilawar with memset */
    memset((void*)dilawar, 1, 1); 
    for(i = 0; i < strlen(dilawar); i++)
    {
        printf("|- Var 'dilawar[%d]' is %d\n", i, dilawar[i]);
    }
/*  
    myNode* pMyNode1 = calloc(400, sizeof(myNode));
    myNode* pMyNode2 = malloc(131*sizeof(myNode));
*/
    printf("\nnodeAvail is %d", nodeAvail);
   // register myNode* pMyNode1 = nodeAvail;
   // register myNode* pMyNode2 = nodeAvail + 1;
    
    /*  check if node is initialised to something */
    printf("\n Nodes are : %d, %d and the diff is : %d(%d)", pMyNode1, pMyNode2, ((int)pMyNode2 - (int)pMyNode1), sizeof(myNode));
    free(pMyNode1); //free(pMyNode2);
#if 0
    /*  following loop fails to terminate */
    while(j == 0 || loop < 999999999999)
    {
        for(i = 0; i < strlen(dilawar); i++)
        {
            printf("|- Var 'dilawar[i]' is %d", dilawar[i]);
            j++;
        }
        loop++;
    }
    printf("Ah!, finally got a NULL char after %d trial.", loop);
#endif

    return 0;

}

